package com.thrd.taiexhibition.activity;

import android.view.Gravity;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.PagerSnapHelper;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SnapHelper;

import com.airbnb.lottie.LottieAnimationView;
import com.airbnb.lottie.LottieListener;
import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.thrd.basicframework.bean.CommonPopupWindow;
import com.thrd.taiexhibition.R;
import com.thrd.taiexhibition.adapter.FocusIndustryAdapter;
import com.thrd.taiexhibition.base.BackTitleActivity;
import com.thrd.taiexhibition.bean.BeanCityIndustryDetail;
import com.thrd.taiexhibition.fresco.FrescoImage;
import com.thrd.taiexhibition.fresco.FrescoUtils;
import com.thrd.taiexhibition.presener.CityFocusIndustryImagePresener;
import com.thrd.taiexhibition.ui.FocusIndustryInfoView;
import com.thrd.taiexhibition.utils.Constant;
import com.thrd.taiexhibition.view.CityFocusIndustryImage;

import java.util.Arrays;
import java.util.List;

/**
 * @author: ${唐杰}
 * @date: 2021/3/26
 */
@Route(path = "/app/fiia",group = "app")
public class FocusIndustryImageActivity extends BackTitleActivity implements CityFocusIndustryImage {
    @Autowired(name="image")
    String image;
    @Autowired(name="id")
    long id;
    @Autowired(name="industryDetailBackType")
    int industryDetailBackType;


    private FocusIndustryInfoView mFocusIndustryInfoView;
    private CityFocusIndustryImagePresener mCityFocusIndustryImagePresener;
    //    private RecyclerView mRecyclerView;
    private FocusIndustryAdapter mFocusIndustryAdapter;
    private List<String> listStrings;
    private LottieAnimationView mLottieAnimationView;
    private FrescoImage frescoImage;


    @Override
    protected int getLayout() {
        return R.layout.activity_focus_industry_image;
    }

    @Override
    protected void initView() {
        mFocusIndustryInfoView = (FocusIndustryInfoView) findViewById(R.id.tiv_image);
        mLottieAnimationView = (LottieAnimationView) findViewById(R.id.lottie_view);
        frescoImage = (FrescoImage) findViewById(R.id.fi_image);
        if(industryDetailBackType==0){
            FrescoUtils.getInstance().setPrefixProgressLoad(this, frescoImage, image);
        }else{
            mLottieAnimationView.setAnimationFromUrl(Constant.IMAGE+image);
            mLottieAnimationView.setFailureListener(new LottieListener<Throwable>() {
                @Override
                public void onResult(Throwable result) {
                    result.printStackTrace();
                }
            });
            mLottieAnimationView.playAnimation();

        }
        // mRecyclerView = (RecyclerView) findViewById(R.id.rv_recyclerview);
//        LinearLayoutManager manager=new LinearLayoutManager(this);
//        manager.setOrientation(RecyclerView.HORIZONTAL);
//        mRecyclerView.setLayoutManager(manager);
//        SnapHelper snapHelper=new PagerSnapHelper();
//        snapHelper.attachToRecyclerView(mRecyclerView);


    }

    @Override
    protected void initData() {
        mCityFocusIndustryImagePresener = new CityFocusIndustryImagePresener(this);
        mCityFocusIndustryImagePresener.getCityFocusData(id);



    }



    @Override
    protected void initEvent() {
        mFocusIndustryInfoView.setOnTouchImageListener(new FocusIndustryInfoView.OnTouchImageListener() {
            @Override
            public void onTouchImageListener(BeanCityIndustryDetail mBeanCityIndustryDetail) {
                //  setDialog(mBeanCityIndustryDetail.getIndustryDetailImgs());
                if(mBeanCityIndustryDetail.getIsSkip()==0){
                    setDialog( mBeanCityIndustryDetail.getIndustryDetailImgs());
                }else{
                    ARouter.getInstance().build("/app/cidca")
                            .withString("industryDetailCompanyLocationImg",mBeanCityIndustryDetail.getIndustryDetailCompanyBackResource())
                            .withLong("id",mBeanCityIndustryDetail.getId())
                            .navigation();
                }

                //  ToastUtil.show(FocusIndustryImageActivity.this,"点击");
                //
            }
        });
    }

    @Override
    public void onError(String error, int type) {

    }

    private List<BeanCityIndustryDetail> mBeanHotActivityList;
    @Override
    public void onSuccess(List<BeanCityIndustryDetail> mBeanHotActivityList) {
        this.mBeanHotActivityList=mBeanHotActivityList;
        mFocusIndustryInfoView.setTouchData(image,mBeanHotActivityList);
    }

    private CommonPopupWindow.Builder mBuilderCity;
    private CommonPopupWindow mCommonPopupWindowCity;
    private void setDialog(String str){
        //用逗号将字符串分开，得到字符串数组
        String[] strs=str.split(",");
        //将字符串数组转换成集合list
        listStrings = Arrays.asList(strs);
        if(mBuilderCity==null) {
            mBuilderCity = new CommonPopupWindow.Builder(this);
            mBuilderCity.setView(R.layout.popup_home_industry);
            mBuilderCity.setOutsideTouchable(true);
            mBuilderCity.setWidthAndHeight(getResources().getDimensionPixelSize(R.dimen.qb_px_184), getResources().getDimensionPixelSize(R.dimen.qb_px_247));
            mBuilderCity.setViewOnclickListener(new CommonPopupWindow.ViewInterface() {
                @Override
                public void getChildView(View view, int layoutResId) {



                }
            });

        }

        if(mFocusIndustryAdapter!=null){
            mFocusIndustryAdapter.refresh(listStrings);
        }

        if(mCommonPopupWindowCity==null){
            mCommonPopupWindowCity = mBuilderCity.create();
            View view=mCommonPopupWindowCity.getContentView();
            RecyclerView mRecyclerView= view.findViewById(R.id.rv_recyclerview);
            LinearLayoutManager manager=new LinearLayoutManager(FocusIndustryImageActivity.this);
            manager.setOrientation(RecyclerView.HORIZONTAL);
            mRecyclerView.setLayoutManager(manager);
            SnapHelper snapHelper=new PagerSnapHelper();
            snapHelper.attachToRecyclerView(mRecyclerView);
            if(mFocusIndustryAdapter==null){
                mFocusIndustryAdapter = new FocusIndustryAdapter(FocusIndustryImageActivity.this,listStrings);
                mRecyclerView.setAdapter(mFocusIndustryAdapter);
            }else{
                mFocusIndustryAdapter.notifyDataSetChanged();
            }

        }


        if(mCommonPopupWindowCity!=null)
            mCommonPopupWindowCity.showAtLocation(getWindow().getDecorView(),Gravity.CENTER);
    }






}
